import dayjs from 'dayjs'

// 校验邮箱
export const emailValidator = (rule, value, callback) => {
  const mailReg = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/
  if (!value) {
    callback(new Error('请输入邮箱'))
  } else if (mailReg.test(value)) {
    callback()
  } else {
    callback(new Error('请输入正确的邮箱格式'))
  }
}

// 校验手机号
export const phoneValidator = (rule, value, callback) => {
  const phoneReg = /^1[3|4|5|6|7|8|9][0-9]\d{8}$/
  if (!value) {
    callback(new Error('请输入手机号码'))
  } else if (phoneReg.test(value)) {
    callback()
  } else {
    callback(new Error('请输入正确的手机号码'))
  }
}

// 校验只能输入数字 字母 下划线
export const numberLetterUnderline = (rule, value, callback) => {
  const stringReg = /[A-Za-z0-9-\_]$/g
  if (!value) {
    callback(new Error('请输入编码'))
  } else if (stringReg.test(value)) {
    callback()
  } else {
    callback(new Error('只能输入字母数字下划线'))
  }
}

// 校验座机
// export const telValidator = (rule, value, callback) => {
//   const telReg = /^((0\d{2,3}-\d{7,8})|(1[3584]\d{9}))$/
//   if (value === '') {
//     callback(new Error('请输入办公电话'))
//   } else if (telReg.test(value)) {
//     callback()
//   } else {
//     callback(new Error('请输入正确办公电话'))
//   }
// }

// 开始日期不可选择
export const disabledStartDate = (startValue, endValue, currentDate) => {
  let calendarDate = dayjs(startValue).format('YYYY-MM-DD')
  if (!endValue) {
    return currentDate ? currentDate < calendarDate : false
  }
  return calendarDate >= endValue
}
// 结束日期不可选择
export const disabledEndDate = (endValue, startValue, currentDate) => {
  let calendarDate = dayjs(endValue).format('YYYY-MM-DD')
  if (!startValue) {
    return currentDate ? currentDate < calendarDate : false
  }
  return startValue >= calendarDate || currentDate < calendarDate
}

// 未来日期不可选择
export const disabledDateFuture = current => {
  return current && current > dayjs().endOf('day')
}

// 某日期之前不可选择
export const disabledDateOld = (current, day) => {
  return current && current < day
}
// 选择区间日期
export const disabledDateRange = (current, endDay, startDay) => {
  if (endDay) return current <= startDay || current >= endDay
  else return current <= startDay
}
// 选择区间以外日期
export const disabledDateRangeOutside = (current, startDay, endDay) => {
  return current >= startDay && current <= endDay
}
// 区间日期之间所有日期
export const dateRangeAll = (startDay, endDay) => {
  const date = []
  const start = new Date(startDay)
  const end = new Date(endDay)
  while (start <= end) {
    date.push(start.getFullYear() + '-' + (start.getMonth() + 1).toString().padStart(2, 0) + '-' + start.getDate().toString().padStart(2, 0))
    start.setDate(start.getDate() + 1)
  }
  return date
}

// 多个日期返回最大值
export const getMaxDate = dates => {
  let maxDate = dates[0]
  for (let i = 1; i < dates.length; i++) {
    if (dates[i] > maxDate) {
      maxDate = dates[i]
    }
  }
  return maxDate
}

// 1返回某月所有日期以及星期 2返回区间所有日期以及星期
export const getMonthDatesAndDays = (data1, data2, num) => {
  const startOfMonth = num === 1 ? new Date(data1, data2 - 1, 1) : new Date(data1)
  const endOfMonth = num === 1 ? new Date(data1, data2, 0) : new Date(data2)
  const dates = []
  const days = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六']
  while (startOfMonth <= endOfMonth) {
    const dayOfWeek = startOfMonth.getDay()
    const date = new Date(startOfMonth).getDate().toString().padStart(2, 0)
    dates.push({ title: date + '/' + days[dayOfWeek] })
    startOfMonth.setDate(startOfMonth.getDate() + 1)
  }
  return dates
}

export const getUrlParam = (url) =>{
  let arrObj = url.split('?')
  let retObj = null
  if (arrObj.length > 1) {
    retObj = {}
    retObj['retUrl'] = arrObj[0]
    let arrPara = arrObj[1].split('&')
    let arr
    for (let i = 0; i < arrPara.length; i++) {
      arr = arrPara[i].split('=')
      retObj[arr[0]] = arr[1]
    }
    return retObj
  } else {
    return retObj
  }
}